﻿/// <reference path="../../_references.js" />
(function () {
    angular.module('TfsApp').service('TaskService', function () {
        function TaskService() {
            this.userStories = this.getUserStories();
            this.tasks = this.getTasks();                        
        }

        TaskService.prototype.getTasks = function () {
            var tasks = [];
            tasks.push({
                id: 1,
                userStoryId: 1,
                title: 'Oms - Danfe - Corrigir Titulo',
                estimatedWork: 3,
                remainingWork: 5,
                completedWork: 10,
                state: 'In Progress'
            }, {
                id: 2,
                userStoryId: 2,
                title: 'Criar Service',
                estimatedWork: 3,
                remainingWork: 2,
                completedWork: 11,
                state: 'In Progress'
            }, {
                id: 3,
                userStoryId: 2,
                title: 'Criar Controller',
                estimatedWork: 3,
                remainingWork: 4,
                completedWork: 10,
                state: 'In Progress'
            });

            return tasks;
        };

        TaskService.prototype.getUserStories = function () {
            var stories = [];
            stories.push({
                id: 1,
                title: 'Bugs Gerais'
            },
            {
                id: 2,
                title: 'Ocorrências'
            });

            return stories;
        };

        TaskService.prototype.getTasksGrouped = function () {
            var _this = this;
            var result = Enumerable.From(this.userStories).Select(function (u) { return { userStoryId: u.id, title: u.title }; }).ToArray();
            Enumerable.From(result).ForEach(function (r) {
                r.tasks = Enumerable.From(_this.tasks).Where(function (t) { return t.userStoryId == r.userStoryId }).ToArray();
            });

            return result;
        };

        TaskService.prototype.getNewTask = function () {
            return {
                id: 0,
                title: 'Nova Tarefa',
                userStoryId: 0,
                estimatedWork: 0,
                remainingWork: 0,
                completedWork: 0
            };
        };

        TaskService.prototype.getAvailableUserStories = function () {
            return Enumerable.From(this.userStories).Select(function (u) { return { id: u.id, title: u.title }; }).ToArray();
        };

        TaskService.prototype.saveTask = function (task) {
            var dbTask = Enumerable.From(this.tasks).FirstOrDefault(null, function (t) { return t.id === task.id; });
            if (dbTask === null) {
                this.tasks.push(task);                
            }
            else {
                angular.copy(task, dbTask);
            }
        };

        TaskService.prototype.getTaskById = function (id) {
            id = parseInt(id);
            return Enumerable.From(this.tasks).FirstOrDefault(null, function (t) { return t.id === id; });
        };

        return new TaskService();
    });
})();